package top.leyi.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import top.leyi.entity.user.User;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtil {
    public static  final String secret = "clearlove";
    ///过期时间设置为30分钟
    public static  final long  ttlMillis = 1000*60*30;
    /**
     * @description 生成JWT
     * @return      Java Web Token
     */
    public static String createJWT(User user){
        //指定签名的时候使用的签名算法，也就是header那部分，jjwt已经将这部分内容封装好了。
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

        //生成JWT的时间
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);

        //创建payload
        Map<String, Object> claims = new HashMap<>();
        claims.put("openId", user.getOpenId());
        claims.put("id", user.getId());
        JwtBuilder builder = Jwts.builder().setClaims(claims)
                                 .setIssuedAt(now)
                                 .signWith(signatureAlgorithm, secret);

        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            //设置过期时间
            builder.setExpiration(exp);
        }
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            //设置过期时间
            builder.setExpiration(exp);
        }
        return builder.compact();
    }


    /**
     * Token的解密
     * @param token 加密后的token
     * @return
     */
    public static Claims parseJWT(String token ) {
        //签名秘钥，和生成的签名的秘钥一模一样

        //得到DefaultJwtParser
        Claims claims = Jwts.parser()
                //设置签名的秘钥
                .setSigningKey(secret)
                //设置需要解析的jwt
                .parseClaimsJws(token).getBody();
        return claims;
    }
    /**
     * Token的解密
     * @param token 加密后的token
     * @param user  用户的对象
     * @return
     */
    public static User parseJWTToUser(String token) {

        //得到DefaultJwtParser
        Claims claims = Jwts.parser()
                //设置签名的秘钥
                .setSigningKey(secret)
                //设置需要解析的jwt
                .parseClaimsJws(token).getBody();

        User user = new User();
        user.setOpenId(claims.get("openId").toString());
        return user;
    }
    public static Boolean isVerify(String token, User user){
        Claims claims =  parseJWT(token);
        return claims != null && claims.get("openId").equals(user.getOpenId());
    }

    /**
     * @description test
     * @param args
     */
    public static  void main(String[] args){
        User user0 = new User();
        String token = createJWT(user0);

        System.out.println(token);
        Claims claims = parseJWT(token);
        System.out.println(claims.get("openId"));
        User user = new User();
        user.setOpenId("clearlove");
        System.out.println(isVerify(token,user ));
        System.out.println(token.length());
    }
}
